package com.vinjogames.som;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.vinjogames.gfx.AnimationManager;
import com.vinjogames.gfx.Camera;
import com.vinjogames.gfx.GameView;
import com.vinjogames.som.entities.Game;
import com.vinjogames.som.entities.Map;
import com.vinjogames.util.GameClock;
import com.vinjogames.util.GameThread;
import com.vinjogames.util.InputManager;
import com.vinjogames.util.Logger;

public class GameActivity extends Activity {
	GameView mView;
	Game mGame;
	GameThread mThread;
	InputManager mInputManager;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setUpDisplay();
		
		Logger.setContext(this);
		//Preferences.setUpPrefs(this);
		
		mGame = new Game();
		//mMenu = new TitleMenu();
		mView = new GameView(this, mGame);
		mThread = new GameThread(mGame);
		mInputManager = InputManager.getInstance();
		mInputManager.init(this, mGame);
		Camera.getInstance().load(Map.ZONE_WIDTH, Map.ZONE_HEIGHT);
		
		setContentView(mView);
		mView.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent e) {
				try {
					mInputManager.handleTouchEvent(e);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
				return true;
			}
		});
		
		mView.loadAllResources();
		mThread.start();
	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.main, menu);
//		return true;
//	}
	private void setUpDisplay(){
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		float width = 0;
		float height = 0;
		
		//This is to flip the screen //zachs bug if you start game with screen off
		if(metrics.widthPixels > metrics.heightPixels){
			width = metrics.widthPixels;
			height = metrics.heightPixels;
		}else{
			width = metrics.heightPixels;
			height = metrics.widthPixels;
		}
		
		float ratio = width/height;
		Camera.getInstance().setGLWIDTH(Camera.GL_HEIGHT*ratio);
		Camera.getInstance().setScreenSize((int)width,(int)height);
	}
	public static Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) { 
			super.handleMessage(msg);
			String text = msg.getData().getString("Text");
			Logger.popUp(text);
		}
	};
	
	@Override
	protected void onPause(){
		super.onPause();//Pauses Activity Thread
		mView.onPause();//Pauses Renderer Thread
		mThread.pause();//Pauses GameThread

		//mConnectionManager.close();//TODO:  
		Logger.log("[Activity]: onPause");
	}

	@Override
	protected void onResume(){
		super.onResume();
		mView.onResume();
		mThread.unpause();
		Logger.log("[Activity]: onResume");
	}
	
	@Override
	public void onBackPressed() {
		getUserConfirmation();
	}
	
	private void getUserConfirmation(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Do you really want to exit?");
		builder.setCancelable(false);
		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				exit();
			}
		});
		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		builder.create();
		builder.show();
	}
	
	public void exit(){
		//res.PauseMusic();
		//res.vibrator.cancel();
		//saveGame(mGame.getSaveState());
        Logger.log("[Activity]: onBackPressed " + GameClock.getInstance().getTime());
		mThread.close();
        //mConnectionManager.close();
        AnimationManager.getInstance().clearMemory();
        finish();
	}
}
